package fun.xinnix.service.impl;

import fun.xinnix.service.IotService;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IotServiceImpl implements IotService {
    @Resource
    MqttClient mqttClient;

    @Override
    public void controlLed(Boolean status) {
        // 创建消息并设置 QoS
        String content = Boolean.TRUE.equals(status) ? "1" : "0";
        MqttMessage message = new MqttMessage();
        message.setPayload(content.getBytes());
        message.setQos(1);
        try {
            mqttClient.publish("led01", message);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }
}


